package com.example.ymm.modules1.service.impl.user;

import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.example.ymm.base.BaseServiceImpl;
import com.example.ymm.modules1.entity.my.user.DbIntegralLog;
import com.example.ymm.modules1.entity.my.user.DbManqinLog;
import com.example.ymm.modules1.entity.my.user.DbUser;
import com.example.ymm.modules1.mapper.my.user.DbManqinLogMapper;
import com.example.ymm.modules1.query.DbManqinLogQueryCriteria;
import com.example.ymm.modules1.service.dd.DingMiniNoticeService;
import com.example.ymm.modules1.service.dto.ManqinFaFanDto;
import com.example.ymm.modules1.service.dto.ManqinLogDto;
import com.example.ymm.modules1.service.user.DbIntegralLogService;
import com.example.ymm.modules1.service.user.DbManqinLogService;
import com.example.ymm.modules1.service.user.DbUserService;
import com.example.ymm.util.DateKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

@Service
@DSTransactional
@Slf4j
public class DbManqinLogServiceImpl extends BaseServiceImpl<DbManqinLogMapper, DbManqinLog> implements DbManqinLogService {

    @Autowired
    DbIntegralLogService dbIntegralLogService;

    @Autowired
    DbUserService dbUserService;
    @Autowired
    DingMiniNoticeService dingMiniNoticeService;

    @Value("${spring.profiles.active}")
    String PD_MATCH;

    @Override
    public List<ManqinLogDto> getManqinList(DbManqinLogQueryCriteria criteria) {
        return baseMapper.getManqinList(criteria);
    }


    @Override
    public void fafangNianManqin(ManqinFaFanDto dto) {
        DbManqinLogQueryCriteria criteria=new DbManqinLogQueryCriteria();
        criteria.setUserId(dto.getUserId());
        Date now = new Date();
        String month = DateKit.dateFormat(now, "yyyy-MM");
        String year = DateKit.dateFormat(now, "yyyy");
        criteria.setYear(year);
        String remark=null;
        Integer integral=null;
        //1 年满勤 2 年全勤
        if(dto.getType().equals(1)){
            criteria.setIsFullYear(1);
            remark="年满勤";
            integral=50;
        }else if(dto.getType().equals(2)){
            criteria.setIsAllYear(1);
            remark="年全勤";
            integral=100;
        }
        List<ManqinLogDto> manqinList = getManqinList(criteria);
        log.info("--满勤记录人数:"+manqinList.size());
        for (ManqinLogDto manqinLogDto : manqinList) {
            DbManqinLog manqinLog = new DbManqinLog();
            manqinLog.setMonth(month);
            manqinLog.setYear(year);
            manqinLog.setUserId(manqinLogDto.getUserId());
            manqinLog.setRemark(remark);
            manqinLog.setIsAllYear(criteria.getIsAllYear());
            manqinLog.setIsFullYear(criteria.getIsFullYear());
            manqinLog.setUserName(manqinLogDto.getUserName());


            DbIntegralLog integralLog = new DbIntegralLog();
            integralLog.setIntegral(integral);
            integralLog.setReadType(1);
            integralLog.setUserId(manqinLogDto.getUserId());
            integralLog.setUserName(manqinLogDto.getUserName());
            integralLog.setNote(remark);
            integralLog.setSysNote("系统结算年满勤或年全勤");
            integralLog.setType(1);

            Timestamp countTime = new Timestamp(now.getTime());
            integralLog.setCreateTime(countTime);
            dbIntegralLogService. saveOrUpdate(integralLog);


            //保存
            manqinLog.setIntegralLogId(integralLog.getIntegralId());
            saveOrUpdate(manqinLog);

            // 添加积分 补卡满勤积分加入
            DbUser user = dbUserService.getById(manqinLogDto.getUserId());
            Integer total = user.getIntegral() + integral;
            user.setIntegral(total);
            dbUserService.saveOrUpdate(user);

            System.out.println("--userName:"+user.getUserName()+"--增加积分:"+integral);
            System.out.println("--积分id："+integralLog.getIntegralId());
            //发送消息
            if(!"dev".equals(PD_MATCH)){
                dingMiniNoticeService.sendReadIntegralMsg(integralLog);
            }else{
                System.out.println("--本地模拟发送消息"+integralLog);
            }


        }
    }
}
